﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Web;
using System.Web.Mvc;
using VVA.Data.Service;

namespace System.Web.Mvc
{
    public static class HtmlHelperExtension
    {
        public static MvcHtmlString Slide(this HtmlHelper helper)
        {
            var dir = HttpContext.Current.Server.MapPath("~/Upload/Slide");
            var result = string.Empty;

            if (Directory.Exists(dir))
            {
                var sb = new StringBuilder();
                var lstImage = Directory.EnumerateFiles(dir, "*.png");

                sb.Append("<div class=\"slide\">");

                for (int i = 0; i < lstImage.Count(); i++)
                {
                    var fileName = lstImage.ElementAt(i);
                    var lastIndexOfSeperator = fileName.LastIndexOf(@"\");
                    fileName = fileName.Remove(0, lastIndexOfSeperator + 1);
                    sb.AppendFormat("<img src=\"{0}\" />", "/Upload/Slide/" + fileName);
                }

                sb.Append("</div>");

                result = sb.ToString();
            }

            return new MvcHtmlString(result);
        }

        public static MvcHtmlString Audio(this HtmlHelper helper, bool autoPlay, bool loop)
        {
            var dir = HttpContext.Current.Server.MapPath("~/Upload/Audio");
            var result = string.Empty;

            if (Directory.Exists(dir))
            {
                var sb = new StringBuilder();
                var lstImage = Directory.EnumerateFiles(dir, "*.mp3");

                var strLoop = loop ? "loop=\"loop\"" : string.Empty;
                var strAutoPlay = autoPlay ? "autoplay=\"autoplay\"" : string.Empty;

                var strMuted = "";

                sb.AppendFormat("<audio {0} {1} class=\"bg-audio\" {2}>", strLoop, strAutoPlay, strMuted);

                for (int i = 0; i < lstImage.Count(); i++)
                {
                    var fileName = lstImage.ElementAt(i);
                    var lastIndexOfSeperator = fileName.LastIndexOf(@"\");
                    fileName = fileName.Remove(0, lastIndexOfSeperator + 1);
                    sb.AppendFormat("<source src=\"{0}\" type=\"{1}\">", "/Upload/Audio/" + fileName, "audio/mpeg");
                }

                sb.Append("</audio>");
                result = sb.ToString();
            }

            return new MvcHtmlString(result);
        }

        public static MvcHtmlString ProductCategoryMenu(this HtmlHelper helper, string routeName)
        {
            var service = new TaxonomyService();

            var sb = new StringBuilder();
            sb.Append("<ul>");

            var lst = service.GetAll("Product", helper.ViewContext.RouteData.Values["culture"].ToString());

            foreach (var item in lst)
            {
                sb.AppendFormat("<li>{0}</li>"
                    , HtmlHelper.GenerateRouteLink(helper.ViewContext.RequestContext, helper.RouteCollection, item.TaxonomyName, helper.ViewContext.RouteData.Values["culture"].ToString() == "en-US" ? routeName + "-en" : routeName, new Routing.RouteValueDictionary { { "slug", item.Slug } }, null));
            }

            sb.Append("</ul>");

            return new MvcHtmlString(sb.ToString());
        }

        public static MvcHtmlString ArticleMenu(this HtmlHelper helper, string slug, string routeName)
        {
            var service = new ArticleService();

            var sb = new StringBuilder();
            sb.Append("<ul>");

            var lst = service.GetAllByTaxonomySlug(slug, helper.ViewContext.RouteData.Values["culture"].ToString());

            foreach (var item in lst)
            {
                sb.AppendFormat("<li>{0}</li>"
                    , HtmlHelper.GenerateRouteLink(helper.ViewContext.RequestContext, helper.RouteCollection, item.Title, helper.ViewContext.RouteData.Values["culture"].ToString() == "en-US" ? routeName + "-en" : routeName, new Routing.RouteValueDictionary { { "slug", item.Slug } }, null));
            }

            sb.Append("</ul>");

            return new MvcHtmlString(sb.ToString());
        }

        public static MvcHtmlString NewsCategoryMenu(this HtmlHelper helper, string parentSlug, string routeName)
        {
            var service = new TaxonomyService();

            var sb = new StringBuilder();
            sb.Append("<ul>");

            var lst = service.GetAllByParentSlug(parentSlug, helper.ViewContext.RouteData.Values["culture"].ToString());

            foreach (var item in lst)
            {
                if (item.Slug == "tuyen-dung")
                {
                    sb.AppendFormat("<li>{0}</li>"
                    , HtmlHelper.GenerateRouteLink(helper.ViewContext.RequestContext, helper.RouteCollection, item.TaxonomyName, helper.ViewContext.RouteData.Values["culture"].ToString() == "en-US" ? "Recruitment-en" : "Recruitment", null, null));
                }
                else
                {
                    sb.AppendFormat("<li>{0}</li>"
                    , HtmlHelper.GenerateRouteLink(helper.ViewContext.RequestContext, helper.RouteCollection, item.TaxonomyName, helper.ViewContext.RouteData.Values["culture"].ToString() == "en-US" ? routeName + "-en" : routeName, new Routing.RouteValueDictionary { { "slug", item.Slug } }, null));
                }

            }

            sb.Append("</ul>");

            return new MvcHtmlString(sb.ToString());
        }

        public static MvcHtmlString Pager(this HtmlHelper helper, UrlHelper urlHelper, int pageSize, int totalCount)
        {
            var sb = new StringBuilder();
            var currentPage = int.Parse(helper.ViewContext.RouteData.Values["page"].ToString());
            var pageCount = Math.Ceiling((double)(totalCount / pageSize)) + 1;

            if (pageCount > 1)
            {
                sb.AppendFormat("<div class=\"pagination\"><a href=\"{0}\" title=\"First Page\">&laquo; First</a><a href=\"{1}\" title=\"Previous Page\">&laquo; Previous</a>", GeneratePagingUrl(urlHelper, 1), GeneratePagingUrl(urlHelper, currentPage - 1));
                var isCurrent = true;

                for (int i = 0; i < pageCount; i++)
                {
                    isCurrent = (currentPage == i + 1);
                    sb.AppendFormat("<a href='{0}' id='lnk{1}' class='number {2}' title='{1}'>{1}</a>", GeneratePagingUrl(urlHelper, i + 1), i + 1, isCurrent ? "current" : string.Empty);
                }
                sb.AppendFormat("<a href=\"{0}\" title=\"Next Page\">Next &raquo;</a><a href=\"{1}\" title=\"Last Page\">Last &raquo;</a></div>", GeneratePagingUrl(urlHelper, currentPage + 1), GeneratePagingUrl(urlHelper, (int)pageCount));
            }

            return new MvcHtmlString(sb.ToString());
        }        

        private static string GeneratePagingUrl(UrlHelper url, int page)
        {
            var _result = string.Empty;

            _result = url.Action("Index", new { page = page });

            return _result;
        }
    }
}